package com.yykj.hospital.server.service.impl;

import com.yykj.common.enums.ResultEnums;
import com.yykj.common.exception.NormalException;
import com.yykj.common.util.PageRequestUtil;
import com.yykj.common.vo.PageData;
import com.yykj.hospital.server.dto.HospitalNavigatorTitleListDTO;
import com.yykj.hospital.server.form.HospitalNavigatorEditForm;
import com.yykj.hospital.server.form.HospitalNavigatorListForm;
import com.yykj.hospital.server.model.entity.HospitalGuide;
import com.yykj.hospital.server.model.entity.HospitalNavigator;
import com.yykj.hospital.server.model.repository.HospitalNavigatorRepository;
import com.yykj.hospital.server.service.HospitalNavigatorService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

@Service
public class HospitalNavigatorServiceImpl implements HospitalNavigatorService {


    @Override
    public HospitalNavigator editHospitalNavigator(HospitalNavigatorEditForm form) {
        if(form.getPid() == null){
            form.setPid(0);
        }
        if(form.getType() == null){
            throw new NormalException(ResultEnums.PARAM_ERROR);
        }
        HospitalNavigator hospitalNavigator;
        if (form.getId() != null && form.getId() > 0) {
            hospitalNavigator = navigatorRepository.findById(form.getId()).orElseThrow(() -> new NormalException(ResultEnums.NOT_EXIST));
        } else {
            hospitalNavigator = new HospitalNavigator();
        }
        BeanUtils.copyProperties(form, hospitalNavigator);
        hospitalNavigator = navigatorRepository.save(hospitalNavigator);
        return hospitalNavigator;
    }

    @Override
    public void deleteHospitalNavigator(Integer id) {
        navigatorRepository.deleteById(id);
    }

    @Override
    public PageData<HospitalNavigator> listHospitalNavigator(HospitalNavigatorListForm form) {

        Pageable pageable = PageRequest.of(PageRequestUtil.getPage(form.getCurrentPage()), PageRequestUtil.getSize(form.getPageSize()));
        Page<HospitalNavigator> list = navigatorRepository.findAll(new Specification<HospitalNavigator>() {
            @Override
            public Predicate toPredicate(Root<HospitalNavigator> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
                List<Predicate> predicateList = new ArrayList<>();
                predicateList.add(builder.equal(root.get("hospitalId"), form.getHospitalId()));
                if (!StringUtils.isEmpty(form.getKeyword())) {
                    predicateList.add(builder.like(root.get("title"), "%" + form.getKeyword() + "%"));
                }
                if (form.getType() != null) {
                    predicateList.add(builder.equal(root.get("type"), form.getType()));
                }
                if (form.getStatus() != null) {
                    predicateList.add(builder.equal(root.get("status"), form.getStatus()));
                }

                Predicate[] predicates = new Predicate[predicateList.size()];
                return query.where(predicateList.toArray(predicates)).getRestriction();
            }
        }, pageable);
        return PageData.formatList(list, form.getCurrentPage(), form.getPageSize());
    }

    @Override
    public HospitalNavigator navigatorInfo(Integer id) {
        return navigatorRepository.findById(id).orElseThrow(() -> new NormalException(ResultEnums.NOT_EXIST));
    }

    @Override
    public List<HospitalNavigatorTitleListDTO> listNavigatorTitle(Integer hospitalId) {
        List<HospitalNavigatorTitleListDTO> result = new ArrayList<>();
        List<HospitalNavigator> list = navigatorRepository.findAllByHospitalIdAndStatusOrderBySortingAscAddTimeDesc(hospitalId, true);
        for (HospitalNavigator navigator : list) {
            if (navigator.getPid() == 0) {
                HospitalNavigatorTitleListDTO parent = new HospitalNavigatorTitleListDTO();
                BeanUtils.copyProperties(navigator, parent);
                List<HospitalNavigatorTitleListDTO> childList = new ArrayList<>();
                for (HospitalNavigator child : list) {
                    if (navigator.getId().equals(child.getPid())) {
                        HospitalNavigatorTitleListDTO dto = new HospitalNavigatorTitleListDTO();
                        BeanUtils.copyProperties(child, dto);
                        childList.add(dto);
                    }
                }
                parent.setList(childList);
                result.add(parent);
            }
        }
        return result;
    }

    private HospitalNavigatorRepository navigatorRepository;

    @Autowired
    public void setNoticeRepository(HospitalNavigatorRepository navigatorRepository) {
        this.navigatorRepository = navigatorRepository;
    }
}
